<?php

class GameController extends Controller
{
	/**
	 * @return array action filters
	 */
    
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return $this->getRules();
	}

        
        public function actionIndex(){
            //init page value
            $pagevals = array(
                'page_title' => "Quản trị Games",
                'subpage_title' => 'Danh sách Games',
                //'action' => 'game/abc'
            );
            
            //check input params and validate input
            $config = ConfigModel::model()->findByPk('1')->attributes;
            $paging_limit_number = $config['paging_limit_number'];
            $status = isset($_GET['status']) ? $_GET['status'] : 'active';
            $search_string = isset($_GET['search_string']) ? $_GET['search_string'] : '';
            $page_number = isset($_GET['page_number']) ? intval($_GET['page_number']) : 1;
            $type = isset($_GET['category_id']) ? $_GET['category_id'] : 'all';
            $graphic_type = isset($_GET['graphic_type']) ? $_GET['graphic_type'] : 'all';
            $context = isset($_GET['context']) ? $_GET['context'] : 'all';
            $platform = isset($_GET['platform']) ? $_GET['platform'] : 'all';
            $vote = isset($_GET['vote']) ? $_GET['vote'] : 'all';
            $provider_id = isset($_GET['provider_id']) ? $_GET['provider_id'] : 'all';
            $release_type = isset($_GET['release_type']) ? $_GET['release_type'] : 'all';
            
            $condition = array();
            $sort = array('hot_status'=>'desc','create_time'=>'desc');
            if($status == 'all'){$sort = array('create_time'=>'desc');}
            
            //validate input
            if($page_number <= 0 || is_numeric($page_number)!= true )$page_number=1;
            if($status != 'all'){ $condition['status'] = $status; }
            if($type != 'all'){$condition['category_id'] = $type;}
            if($graphic_type != 'all'){$condition['graphic_type'] = $graphic_type;}
            if($context != 'all'){$condition['context'] = $context;}
            if($platform != 'all'){$condition['platform'] = $platform;}
            if($vote != 'all'){$condition['vote'] = $vote;}
            if($provider_id != 'all'){$condition['provider_id'] = $provider_id;}
            if($release_type != 'all'){$condition['release_type'] = $release_type;}
            
            
            
            if($search_string == null || $search_string == ""){
                $data = GameModel::model()->getByPage($condition, $page_number, $paging_limit_number, $sort);
                $pagevals['total_row'] = GameModel::model()->countRow($condition);
            }else{
                $fields = array('name','homepage','context','category_id','graphic_type','platform','vote');
                $data = GameModel::model()->SearchMultiField($search_string, $condition, $page_number, $paging_limit_number, $fields);
                $pagevals['total_row'] = GameModel::model()->countSearch($search_string, $condition, $fields);
            }
            
            //get in data table
            if(is_array($data)){
                foreach($data as $key => $game){
                    $provider = ProviderModel::model()->getByID($game['provider_id']) ;
                    if($provider != null){ 
                        $data[$key]['provider_name'] = $provider['name'];
                    }else{$data[$key]['provider_name'] = '';}
                    
                    $category = GCategoryModel::model()->getByID($game['category_id']) ;
                    if($category != null){ 
                        $data[$key]['category_name'] = $category['name'];
                    }else{$data[$key]['category_name']='';}
                }
            }

            $filter_fields = array(
                'category_id'=>'asc',
                'context'=>'asc',
                'platform'=>'asc',
                'release_type'=>'asc',
                //'vote'=>'asc',
                //'graphic_type'=>'asc',
                //'status'=>'asc',
            );
            
            //get provider name
            $provider_filter_laber = array();
            $provider_filter_id = GameModel::model()->getUnique('provider_id');
            if(is_array($provider_filter_id)){
                foreach($provider_filter_id as $value){
                    $provider_model = ProviderModel::model()->getByID($value['provider_id']) ;
                    if($provider_model != null){ 
                       $provider_filter_laber[$value['provider_id']]  = $provider_model['name'];
                    }
                }
            }
            //get category name
            $category_filter_laber = array();
            $category_filter_id = GameModel::model()->getUnique('category_id');
            if(is_array($category_filter_id)){
                foreach($category_filter_id as $value){
                    $category_model = GCategoryModel::model()->getByID($value['category_id']) ;
                    if($category_model != null){ 
                       $category_filter_laber[$value['category_id']]  = $category_model['name'];
                    }
                }
            }
            
            //process and render data 
            $pagevals['filter_label'] = $this->getFilterValue($filter_fields);
            $pagevals['filter_label']['provider'] = $provider_filter_laber;
            $pagevals['filter_label']['category'] = $category_filter_laber;
            
            
            $pagevals['page_number'] = $page_number;
            $pagevals['total_page'] = GameModel::model()->countPage($pagevals['total_row'], $paging_limit_number);
            $pagevals['row_start'] = GameModel::model()->get_start_row($pagevals['total_row'], $page_number, $paging_limit_number);
            $pagevals['row_end'] = GameModel::model()->get_end_row($pagevals['total_row'], $page_number, $paging_limit_number);
            $pagevals['status'] = $status;
            $pagevals['search_string'] = $search_string;
            $pagevals['category_id']= $type;
            $pagevals['graphic_type']= $graphic_type;
            $pagevals['provider_id']= $provider_id;
            $pagevals['platform']= $platform;
            $pagevals['vote']= $vote;
            $pagevals['context']= $context;
            $pagevals['release_type']= $release_type;

            $this->display("game/index", array('data'=>$data, 'pagevals'=>$pagevals) );
        }
        
        public function actionCreate()
	{
            $pagevals = array(
                'page_title' => "Quản trị Games",
                'subpage_title' => 'Tạo game mới',
                //'action' => 'game/abc'
            );
            
            
            $selection_fields = array(
                'category_id'=>'asc',
                'context'=>'asc',
                'platform'=>'asc',
                'release_type'=>'asc',
                'screen_style'=>'asc',
                'developer_id'=>'asc',
                //'vote'=>'asc',
                //'graphic_type'=>'asc',
                //'status'=>'asc',
            );
            //get provider name
            $provider_filter_laber = array();
            $providers = ProviderModel::model()->getAll();
            if(is_array($providers)){
                foreach($providers as $value){
                    if($value != null){ 
                       $provider_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }

            //get developer name
            $developer_filter_laber = array();
            $developers = DeveloperModel::model()->getAll();
            if(is_array($developers)){
                foreach($developers as $value){
                    if($value != null){ 
                       $developer_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }
            
            //get category name
            $category_filter_laber = array();
            $categories = GCategoryModel::model()->getAll();
            if(is_array($categories)){
                foreach($categories as $value){
                    if($value != null){ 
                       $category_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }
            
            $pagevals['selection_label'] = $this->getFilterValue($selection_fields);
            $pagevals['selection_label']['provider'] = $provider_filter_laber;
            $pagevals['selection_label']['developer'] = $developer_filter_laber;
            $pagevals['selection_label']['category'] = $category_filter_laber;
            
            ///--------------------------
            $time=time();
            $game = new GameModel;
            if(isset($_POST['name'])){
                if($_POST['new_platform'] != ''){
                    $_POST['platform'] = $_POST['new_platform'];
                }
                if($_POST['new_context'] != ''){
                    $_POST['context'] = $_POST['new_context'];
                }
                if($_POST['new_release_type'] != ''){
                    $_POST['release_type'] = $_POST['new_release_type'];
                }
                //print_r('<pre>');
                //print_r($game->attributes);
                //print_r($_POST);
                //print_r('</pre>');
                //exit();
                $game->attributes = array(
                        'name'=>$_POST['name'],
                        'description'=>$_POST['description'],
                        'short_intro'=>$_POST['short_intro'],
                        'homepage'=>$_POST['homepage'],
                        'provider_id'=>$_POST['provider_id'],
                        'developer_id'=>$_POST['developer_id'],
                        'release_time'=>$_POST['release_time'],
                        'release_type'=>$_POST['release_type'],
                        'hot_status'=>$_POST['hot_status'],
                        'category_id'=>$_POST['category_id'],
                        'context'=>$_POST['context'],
                        'screen_style'=>$_POST['screen_style'],
                        'graphic_type'=>$_POST['graphic_type'],
                        'platform'=>$_POST['platform'],
                        'thumb'=>$_POST['thumb'],
                        'icon'=>$_POST['icon'],
                        'screenshots'=>$_POST['screenshots'],
                        'vote'=>$_POST['vote'],
                        'tags'=>$_POST['tags'],
                        'status'=>$_POST['status'],
                        'create_time'=>$time,
                        'update_time'=>$time,
                    );
                if($game->save(false)){
                    $this->redirect(array('index','status'=>$_POST['status']));
                }
            }
            $this->display("game/create", array('pagevals'=>$pagevals));
	}
        

        public function actionEdit($id)
	{
            if(!isset($_GET['id'])){
                die('Game not found');
            }
                
            $pagevals = array(
                'page_title' => "Quản trị Games",
                'subpage_title' => 'Chỉnh sửa game ',
                //'action' => 'game/abc'
            );
            
            $selection_fields = array(
                'category_id'=>'asc',
                'context'=>'asc',
                'platform'=>'asc',
                'release_type'=>'asc',
                'screen_style'=>'asc',
                'developer_id'=>'asc',
                //'vote'=>'asc',
                //'graphic_type'=>'asc',
                //'status'=>'asc',
            );
            //get provider name
            $provider_filter_laber = array();
            $providers = ProviderModel::model()->getAll();
            if(is_array($providers)){
                foreach($providers as $value){
                    if($value != null){ 
                       $provider_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }

            //get developer name
            $developer_filter_laber = array();
            $developers = DeveloperModel::model()->getAll();
            if(is_array($developers)){
                foreach($developers as $value){
                    if($value != null){ 
                       $developer_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }
            
            //get category name
            $category_filter_laber = array();
            $categories = GCategoryModel::model()->getAll();
            if(is_array($categories)){
                foreach($categories as $value){
                    if($value != null){ 
                       $category_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }
            
            $pagevals['selection_label'] = $this->getFilterValue($selection_fields);
            $pagevals['selection_label']['provider'] = $provider_filter_laber;
            $pagevals['selection_label']['developer'] = $developer_filter_laber;
            $pagevals['selection_label']['category'] = $category_filter_laber;
            
            //-------------------------------
            //get game data
            $game = GameModel::model()->findByPk($id);
            if(!is_object($game)){
                die('Game not found');
            }
            
            ///--------------------------
            $time=time();
            
            if(isset($_POST['name'])){
                if($_POST['new_platform'] != ''){
                    $_POST['platform'] = $_POST['new_platform'];
                }
                if($_POST['new_context'] != ''){
                    $_POST['context'] = $_POST['new_context'];
                }
                if($_POST['new_release_type'] != ''){
                    $_POST['release_type'] = $_POST['new_release_type'];
                }
                
                //print_r('<pre>');
                //print_r($game->attributes);
                //print_r($_POST);
                //print_r('</pre>');
                //exit();
                $game->attributes = array(
                        'name'=>$_POST['name'],
                        'description'=>$_POST['description'],
                        'short_intro'=>$_POST['short_intro'],
                        'homepage'=>$_POST['homepage'],
                        'provider_id'=>$_POST['provider_id'],
                        'developer_id'=>$_POST['developer_id'],
                        'release_time'=>$_POST['release_time'],
                        'release_type'=>$_POST['release_type'],
                        'hot_status'=>$_POST['hot_status'],
                        'category_id'=>$_POST['category_id'],
                        'context'=>$_POST['context'],
                        'screen_style'=>$_POST['screen_style'],
                        'graphic_type'=>$_POST['graphic_type'],
                        'platform'=>$_POST['platform'],
                        'thumb'=>$_POST['thumb'],
                        'icon'=>$_POST['icon'],
                        'screenshots'=>$_POST['screenshots'],
                        'vote'=>$_POST['vote'],
                        'tags'=>$_POST['tags'],
                        'status'=>$_POST['status'],
                        //'create_time'=>$time,
                        'update_time'=>$time,
                    );
                
                if($game->save(false)){
                    $this->redirect(array('index','status'=>$_POST['status']));
                }
            }
            
            $this->display("game/edit", array('pagevals'=>$pagevals,'data'=>$game->attributes));
        }
        
        
        
       
        
        public function doAcion($status,$data){
            $i = 0;
            $time = time();
            $game_id = explode(';', $data);
            if(sizeof($game_id) == 0){
                echo 0;
            }
            else{
                foreach($game_id as $value){
                    if($value != 0){
                        $result = GameModel::model()->modify($value,array('status'=>$status,'update_time'=>$time));
                        if($result == true){
                            $i++;
                        }
                    }
                }
                echo $i;
            }
        }
        
        
        public function actionCopy(){
            if(isset($_POST['game_id'])){
                $i = 0;
                $game_id = explode(';', $_POST['game_id']);
                if(sizeof($game_id) == 0){
                    echo 0;
                }
                else{
                    foreach($game_id as $value){
                        if($value != 0){
                            $result = GameModel::model()->copy($value);
                            if($result == true){
                                $i++;
                            }
                        }
                    }
                    echo $i;
                }
            }
        }
        
        public function actionRemove(){
            if(isset($_POST['remove_ids'])){
                $this->doAcion('deleted', $_POST['remove_ids']);
            }
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'deleted','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionPending(){
            if(isset($_POST['pending_ids'])){
                $this->doAcion('pending', $_POST['pending_ids']);
            }
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionApprove(){
            if(isset($_POST['approve_ids'])){
                $this->doAcion('approved', $_POST['approve_ids']);
            }
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'approved','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionActive(){
            if(isset($_POST['active_ids'])){
                $this->doAcion('active', $_POST['active_ids']);
            }
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'active','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionBlock(){
            if(isset($_POST['block_ids'])){
                $this->doAcion('blocked', $_POST['block_ids']);
            }
            
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'blocked','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionRestore(){
            if(isset($_POST['restore_ids'])){
                $this->doAcion('pending', $_POST['restore_ids']);
            }
            if(isset($_POST['id'])){
                $result = GameModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function getFilterValue($fields){
            if(is_array($fields)){
                $result = array();
                foreach($fields as $key => $value){
                    $temp = GameModel::model()->getUnique($key, $value);
                    if(is_array($temp)){
                        foreach($temp as $val){
                            if($val[$key] !=null && $val[$key]!=''){
                                $result[$key][]= $val[$key];
                            }
                        }
                    }
                }
                return $result;
            }else return null;
        }
        
        public function actionRemoveScreenshot(){
            if(isset($_POST['file_name'])){
                $filename = str_replace(Yii::app()->baseUrl, '', $_POST['file_name']);
                $filename = '.'.$filename;
                //$filename_path = './uploads/images/games/screenshots/'.$_POST['file_name'];
                if(unlink($filename)){echo 'success';}
                else echo 'error';
            }
        }
        
        
        public function actionSearch(){
            $condition = array(
                    'id'=>'> 2',
                    'provider_id'=>' >= 1',
                    'hot_status'=>'1',
                    'vote'=>'> 1',
                    'release_time'=>'> 10000',
                    'graphic_type'=>'2d'
            
            );
            $result = GameModel::model()->filter($condition);
            //print_r('<pre>');
            print_r($condition);
            //print_r('</pre>');
            echo $this->table($result);
            echo '--------------------------------------------------------<br><br>';
            
            $condition1 = array(
                    'id'=>array('and'=>array('>1','<8')),
                    'provider_id'=>array('or'=>array('1','2')),
                    'hot_status'=>'1',
                    'vote'=>'>=3 ',
                    'release_time'=>'< 10000000000',
                    'graphic_type'=>'2d'
            
            );
            $result1 = GameModel::model()->filter($condition1);
            //print_r('<pre>');
            print_r($condition1);
            //print_r('</pre>');
            echo $this->table($result1);
            echo '--------------------------------------------------------<br><br>';
            
            $condition2 = array(
                    'id'=>array('and'=>array('> 5','< 8')),
                    'provider_id'=>'1',
                    'hot_status'=>'1',
                    'vote'=>'> 1 ',
                    'release_time'=>'< 10000000000000',
                    'graphic_type'=>'2d',
                    'platform'=>'web',
            
            );
            $result2 = GameModel::model()->filter($condition2);
            //print_r('<pre>');
            print_r($condition2);
            //print_r('</pre>');
            echo $this->table($result2);
            echo '--------------------------------------------------------<br><br>';
            
            
            $condition3 = array(
                    'id'=>array('and'=>array('> 1','< 8')),
                    'provider_id'=>array('or'=>array('1','2','3')),
                    'hot_status'=>'1',
                    'vote'=>'> 1 ',
                    'release_time'=>'< 10000000000000',
                    //'graphic_type'=>'2d',
                    'status'=>'0'
            
            );
            $result3= GameModel::model()->filter($condition3);
            //print_r('<pre>');
            print_r($condition3);
            //print_r('</pre>');
            echo $this->table($result3);
            echo '--------------------------------------------------------<br><br>';
            
            
            $condition4 = array(
                    'id'=>'> 2',
                    'provider_id'=>array('or'=>array('1','2')),
                    'hot_status'=>'1',
                    //'vote'=>'>=3 ',
                    'release_time'=>'< 1000000000000',
                    'graphic_type'=>'2d',
                    'platform'=>'web',
                    'release_type'=>'Alpha Test'
            
            );
            $result4 = GameModel::model()->filter($condition4);
            //print_r('<pre>');
            print_r($condition4);
            //print_r('</pre>');
            echo $this->table($result4);
            echo '--------------------------------------------------------<br><br>';
            
        }
        
        public function table_condition($condition){
            $string_html = '<table><tr>';
            foreach($condition as $key => $value ){
                $table_string .= "<td>$key</td>";   
            }
            $string_html .= '</tr>';
            
            foreach($condition as $key => $value ){
                $string_html .= '<tr>';
                $table_string .= "<td>$value</td>"; 
                $string_html .= '</tr>';
            } 
            $string_html .= '</table>';
        }
        


        public function table($data_array){
            $table_string ='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            $table_string .= '<table border="1">';
            $table_string .= "<tr>";
            $table_string .= "<td>id</td>";
            $table_string .= "<td>name</td>";
            $table_string .= "<td>description</td>";
            $table_string .= "<td>short_intro</td>";
            $table_string .= "<td>homepage</td>";
            $table_string .= "<td>provider_id</td>";
            $table_string .= "<td>release_time</td>";
            $table_string .= "<td>release_type</td>";
            $table_string .= "<td>hot_status</td>";
            $table_string .= "<td>type</td>";
            $table_string .= "<td>context</td>";
            $table_string .= "<td>screen_style</td>";
            $table_string .= "<td>graphic_type</td>";
            $table_string .= "<td>platform</td>";
            $table_string .= "<td>thumb</td>";
            $table_string .= "<td>icon</td>";
            $table_string .= "<td>screenshots</td>";
            $table_string .= "<td>vote</td>";
            $table_string .= "<td>status</td>";
            $table_string .= "<td>create_time</td>";
            $table_string .= "<td>update_time</td>";
            $table_string .= "</tr>";
            
            if($data_array != null && count($data_array) !=0){
                foreach($data_array as $game){
                    $table_string .= '<tr>';
                    $table_string .= "<td>".$game['id']."</td>";
                    $table_string .= "<td>".$game['name']."</td>";
                    $table_string .= "<td>".$game['description']."</td>";
                    $table_string .= "<td>".$game['short_intro']."</td>";
                    $table_string .= "<td>".$game['homepage']."</td>";
                    $table_string .= "<td>".$game['provider_id']."</td>";
                    $table_string .= "<td>".$game['release_time']."</td>";
                    $table_string .= "<td>".$game['release_type']."</td>";
                    $table_string .= "<td>".$game['hot_status']."</td>";
                    $table_string .= "<td>".$game['category_id']."</td>";
                    $table_string .= "<td>".$game['context']."</td>";
                    $table_string .= "<td>".$game['screen_style']."</td>";
                    $table_string .= "<td>".$game['graphic_type']."</td>";
                    $table_string .= "<td>".$game['platform']."</td>";
                    $table_string .= "<td>".$game['thumb']."</td>";
                    $table_string .= "<td>".$game['icon']."</td>";
                    $table_string .= "<td>".$game['screenshots']."</td>";
                    $table_string .= "<td>".$game['vote']."</td>";
                    $table_string .= "<td>".$game['status']."</td>";
                    $table_string .= "<td>".$game['create_time']."</td>";
                    $table_string .= "<td>".$game['update_time']."</td>";
                    $table_string .= "</tr>";
                }
            }
            $table_string .= "</table>";
            
            return $table_string;
        }
        
        public function actionTest(){
            //$data = GameModel::model()->getByIDs(array('1','2','3','4','5'));
            
            $data = GameModel::model()->getUnique('vote');
            print_r('<pre>');
            print_r($data);
            print_r('</pre>');
        }
        
        public function actionView($id)
	{
            if(!isset($_GET['id'])){
                die('Game not found');
            }
                
            $pagevals = array(
                'page_title' => "Quản trị Games",
                'subpage_title' => 'View game ',
                //'action' => 'game/abc'
            );
            
            
            $selection_fields = array(
                'category_id'=>'asc',
                'context'=>'asc',
                'platform'=>'asc',
                'release_type'=>'asc',
                'screen_style'=>'asc',
                'developer_id'=>'asc',
                //'vote'=>'asc',
                //'graphic_type'=>'asc',
                //'status'=>'asc',
            );
            //get provider name
            $provider_filter_laber = array();
            $provider_filter_id = GameModel::model()->getUnique('provider_id');
            if(is_array($provider_filter_id)){
                foreach($provider_filter_id as $value){
                    $provider_model = ProviderModel::model()->getByID($value['provider_id']) ;
                    if($provider_model != null){ 
                       $provider_filter_laber[$value['provider_id']]  = $provider_model['name'];
                    }
                }
            }
            $pagevals['selection_label'] = $this->getFilterValue($selection_fields);
            $pagevals['selection_label']['provider'] = $provider_filter_laber;
            
            //-------------------------------
            //get game data
            $game = GameModel::model()->findByPk($id);
            if(!is_object($game)){
                die('Game not found');
            }
            

            $data = $game->attributes;
            $pagevals['subpage_title'] = $data['name'];
            
            $this->display("game/edit", array('pagevals'=>$pagevals,'data'=>$data));
        }
}
